// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.SalesWithdraw;
import com.fowo.api.model.sales.withdraw.SalesWithdrawImportPo;
import com.fowo.api.model.sales.withdraw.SalesWithdrawItemVo;
import com.fowo.api.model.sales.withdraw.SalesWithdrawSearchParamPo;
import com.fowo.api.model.sales.withdraw.SalesWithdrawVo;
import com.fowo.api.service.SalesWithdrawService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "销售退货单")
@RequestMapping("/api/sales-withdraw")
@RestController
public class SalesWithdrawController {

  @Resource
  private SalesWithdrawService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<SalesWithdrawItemVo>> search(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<SalesWithdrawItemVo>>> searchOptions(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<SalesWithdrawItemVo>> getPage(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取包括子表的查询结果")
  @GetMapping("page-with-child")
  public R<Page<?>> getPageWithChild(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageWithChildSearch(search));
  }

  @Operation(summary = "仅获取子表的查询结果")
  @GetMapping("page-only-child/{parentId}")
  public R<Page<?>> getPageOnlyChild(
    @PathVariable Long parentId,
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageOnlyChildSearch(parentId, search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<SalesWithdrawVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取系统单号相关的编号列表")
  @GetMapping("global-order-no/{texts}/ids")
  public R<List<Long>> getIdsByGlobalOrderNo(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByGlobalOrderNo(nameList));
  }

  @Operation(summary = "获取编号相关的系统单号列表")
  @GetMapping("global-order-no/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getGlobalOrderNoByIds(idList));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody SalesWithdrawVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody SalesWithdrawVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody SalesWithdrawVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<SalesWithdrawImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('salesWithdrawImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<SalesWithdrawImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(response, "销售退货单导入模版.xlsx");
    InputStream inputStream = new ClassPathResource(
      "templates/SalesWithdraw.xlsx"
    )
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('salesWithdrawExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    SalesWithdrawSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('salesWithdrawBatchSyncBill')")
  @Operation(summary = "同步单据")
  @PostMapping("batchSyncBill")
  public R batchSyncBill(@RequestBody IdListPo po) {
    return R.run(() -> service.batchSyncBill(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawBatchCancellation')")
  @Operation(summary = "批量作废")
  @PostMapping("batchCancellation")
  public R batchCancellation(@RequestBody IdListPo po) {
    return R.run(() -> service.batchCancellation(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawBatchImportreceive')")
  @Operation(summary = "导入收货")
  @PostMapping("batchImportreceive")
  public R batchImportreceive(@RequestBody IdListPo po) {
    return R.run(() -> service.batchImportreceive(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawZuofei')")
  @Operation(summary = "作废")
  @PostMapping("zuofei")
  public R zuofei(@RequestBody IdPo po) {
    return R.run(() -> service.zuofei(po.getId()));
  }

  @PreAuthorize("@am.canAccess('salesWithdrawBatchImportrecord')")
  @Operation(summary = "导入记录")
  @PostMapping("batchImportrecord")
  public R batchImportrecord(@RequestBody IdListPo po) {
    return R.run(() -> service.batchImportrecord(po.getIds()));
  }

  @Operation(summary = "全部")
  @GetMapping("all-orders")
  public R<List<SalesWithdrawItemVo>> allOrders(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allOrders(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("all-ordersPage")
  public R<Page<SalesWithdrawItemVo>> allOrdersPage(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allOrdersPage(search));
  }

  @Operation(summary = "全部包括子表的查询结果")
  @GetMapping("all-ordersPage-with-child")
  public R<Page<?>> allOrdersPageWithChild(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.allOrdersPageWithChildSearch(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("all-ordersLastUpdateTime")
  public R<Date> allOrdersLastUpdateTime(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allOrdersLastUpdateTime(search));
  }

  @Operation(summary = "待收货")
  @GetMapping("wait-received")
  public R<List<SalesWithdrawItemVo>> waitReceived(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitReceived(search));
  }

  @Operation(summary = "待收货分页")
  @GetMapping("wait-receivedPage")
  public R<Page<SalesWithdrawItemVo>> waitReceivedPage(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitReceivedPage(search));
  }

  @Operation(summary = "待收货包括子表的查询结果")
  @GetMapping("wait-receivedPage-with-child")
  public R<Page<?>> waitReceivedPageWithChild(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.waitReceivedPageWithChildSearch(search));
  }

  @Operation(summary = "待收货最后更新时间")
  @GetMapping("wait-receivedLastUpdateTime")
  public R<Date> waitReceivedLastUpdateTime(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitReceivedLastUpdateTime(search));
  }

  @Operation(summary = "已完成")
  @GetMapping("completed")
  public R<List<SalesWithdrawItemVo>> completed(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completed(search));
  }

  @Operation(summary = "已完成分页")
  @GetMapping("completedPage")
  public R<Page<SalesWithdrawItemVo>> completedPage(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedPage(search));
  }

  @Operation(summary = "已完成包括子表的查询结果")
  @GetMapping("completedPage-with-child")
  public R<Page<?>> completedPageWithChild(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.completedPageWithChildSearch(search));
  }

  @Operation(summary = "已完成最后更新时间")
  @GetMapping("completedLastUpdateTime")
  public R<Date> completedLastUpdateTime(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedLastUpdateTime(search));
  }

  @Operation(summary = "已作废")
  @GetMapping("obsolete")
  public R<List<SalesWithdrawItemVo>> obsolete(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.obsolete(search));
  }

  @Operation(summary = "已作废分页")
  @GetMapping("obsoletePage")
  public R<Page<SalesWithdrawItemVo>> obsoletePage(
    SalesWithdrawSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.obsoletePage(search));
  }

  @Operation(summary = "已作废包括子表的查询结果")
  @GetMapping("obsoletePage-with-child")
  public R<Page<?>> obsoletePageWithChild(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.obsoletePageWithChildSearch(search));
  }

  @Operation(summary = "已作废最后更新时间")
  @GetMapping("obsoleteLastUpdateTime")
  public R<Date> obsoleteLastUpdateTime(SalesWithdrawSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.obsoleteLastUpdateTime(search));
  }

  @Operation(summary = "更新状态为2")
  @PostMapping("UpdateSalesWithdrawStatus")
  public R<?> updateSalesWithdrawStatus(@RequestBody JSONObject param) {
    return R.data(() -> service.updateSalesWithdrawStatus(param));
  }

  @Operation(summary = "关联单据(入库单)")
  @PostMapping("GetStockInByRamNo")
  public R<?> getStockInByRamNo(@RequestBody JSONObject param) {
    return R.data(() -> service.getStockInByRamNo(param));
  }

  @Operation(summary = "根据id获取主子表信息")
  @PostMapping("GetSalesWithdrawOrdersById")
  public R<?> getSalesWithdrawOrdersById(@RequestBody JSONObject param) {
    return R.data(() -> service.getSalesWithdrawOrdersById(param));
  }
}
